﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BaseData_CommentLevelEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private Model.EVAL_LEVEL Level
    {
        get
        {
            Model.EVAL_LEVEL level = null;
            if (ViewState["Level"] != null)
            {
                level = (Model.EVAL_LEVEL)ViewState["Level"];
            }
            return level;
        }
        set
        {
            ViewState["Level"] = value;
        }
    }

    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200300102");
        if (!IsPostBack)
        {
            if (this.Code != "")
            {
                Label1.Text = "编辑评价等级";
                this.Level = new BLL.EVAL_LEVEL().GetModel(this.Code);
                if (this.Level != null)
                {
                    txtCode.Text = this.Level.EL_CODE;
                    txtName.Text = this.Level.EL_NAME;
                    txtCode.Enabled = false;
                    this.IsAdd = false;
                    btnAddOrUpdate.Text = "更新";
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='ProductCommentLevel.aspx';</script>");
                }
            }
            else
            {
                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
            }
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消" + btnAddOrUpdate.Text + "并返回上一页吗?'))return false;");
        }
    }
    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (this.IsAdd == true)
        {
            if (Check.CheckPermission("200300102", "00001"))
            {
                if (txtCode.Text != "")
                {
                    Model.EVAL_LEVEL level = new BLL.EVAL_LEVEL().GetModel(txtCode.Text.Trim());
                    if (level == null)
                    {
                        level = new Model.EVAL_LEVEL();
                        level.EL_CODE = txtCode.Text;
                        level.EL_NAME = txtName.Text;
                        if (new BLL.EVAL_LEVEL().Add(level) == level.EL_CODE)
                        {
                            Share.InsertLog("200300102", "00001");
                            Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('添加记录成功');location.href='ProductCommentLevel.aspx';</script>");
                        }
                        else
                        {
                            Share.Alert(this.Page, "添加记录失败,请重新添加。");
                        }
                    }
                    else
                    {
                        Share.Alert(this.Page, "已经存在代码为" + txtCode.Text.Trim() + "的记录!");
                    }
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('请输入代码。');</script>");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='ProductCommentLevel.aspx';</script>");
            }
        }
        else
        {
            if (Check.CheckPermission("200300102", "00002"))
            {
                this.Level.EL_NAME = txtName.Text.ToString();
                if (new BLL.EVAL_LEVEL().Update(this.Level))
                {
                    Share.InsertLog("200300102", "00002");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('更新记录成功');location.href='ProductCommentLevel.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page,"更新记录失败,请重新编辑更新。");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='ProductCommentLevel.aspx';</script>");
            }
            
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductCommentLevel.aspx");
    }
}
